package net.gilgame.bukkit.commands;

import java.util.List;
import net.gilgame.bukkit.lib.Groups;
import net.gilgame.bukkit.lib.Messages;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

/**
 *
 * @author Gilgame
 * @date 28 March 2011
 */
public class CountEntityNear implements ICommand
{
    public boolean run(CommandSender sender, String[] args)
    {
        if (!(sender instanceof Player))
        {
            sender.sendMessage(Messages.NoConsole);
            return true;
        }

        if (args.length > 2 || args.length < 2)
        {
            sender.sendMessage(Messages.InvalidArguments);
            return false;
        }

        int total = 0;
        int radius = 0;
        String name = args[0].toLowerCase();

        try
        {
            radius = Integer.parseInt(args[1]);
        }
        catch (Exception e)
        {
            sender.sendMessage(Messages.InvalidArguments);
            return false;
        }

        List<Entity> entities = ((Player)sender).getNearbyEntities(radius, radius, radius);
        total = count(entities, name);

        String message = String.format(Messages.FoundEntities, total, name);
        sender.sendMessage(message);

        return true;
    }

    private int count(List<Entity> entities, String name)
    {
        int count = 0;

        if (name.equals("animals"))
        {
            for (Entity entity : entities)
            {
                for (String animal : Groups.Animals)
                {
                    if (entity.getClass().getName().toLowerCase().contains(animal))
                    {
                        count++;
                    }
                }
            }
        }
        else if (name.equals("monsters"))
        {
            for (Entity entity : entities)
            {
                for (String monster : Groups.Monsters)
                {
                    if (entity.getClass().getName().toLowerCase().contains(monster))
                    {
                        count++;
                    }
                }
            }
        }
        else if (name.equals("denizens"))
        {
            for (Entity entity : entities)
            {
                for (String denizen : Groups.Denizens)
                {
                    if (entity.getClass().getName().toLowerCase().contains(denizen))
                    {
                        count++;
                    }
                }
            }
        }
        else if (name.equals("projectiles"))
        {
            for (Entity entity : entities)
            {
                for (String projectile : Groups.Projectiles)
                {
                    if (entity.getClass().getName().toLowerCase().contains(projectile))
                    {
                        count++;
                    }
                }
            }
        }
        else if (name.equals("environment"))
        {
            for (Entity entity : entities)
            {
                for (String environment : Groups.Environment)
                {
                    if (entity.getClass().getName().toLowerCase().contains(environment))
                    {
                        count++;
                    }
                }
            }
        }
        else
        {
            for (Entity entity : entities)
            {
                if (entity.getClass().getName().toLowerCase().contains(name))
                {
                    count++;
                }
            }
        }

        return count;
    }
}
